﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.Design;
using System.ComponentModel.Design;
using Org.Eurekaa.DNJ.UI.Design.WebControls;
using System.IO;
using Org.Eurekaa.DNJ.Helpers;
using Org.Eurekaa.DNJ.UI.DNJSettings;
using Org.Eurekaa.DNJ.Settings;

namespace Org.Eurekaa.DNJ.UI.WebControls
{
    [ToolboxBitmap(typeof(Org.Eurekaa.DNJ.UI.Resources.icons.DNJUIIcons), "DNJDialog.bmp")]
    [ToolboxData("<{0}:DNJDialog runat=server Width=300px></{0}:DNJDialog>")]
    [DefaultProperty("Content"),
    ParseChildren(true, "Content"),
    PersistChildren(false),    
    Designer(typeof(DNJBoxControlDesigner))]

    public class DNJDialog : JQueryUIControl
    {
        private DNJDialogSettings _settings = new DNJDialogSettings();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Settings")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJDialogSettings Settings
        {
            get { return _settings; }
            set { _settings = value; }
        }

        private DNJDialogEvents _events = new DNJDialogEvents();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Events")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJDialogEvents ClientEvents
        {
            get { return _events; }
            set { _events = value; }
        }


        private String _title = "DNJ Dialog";
        public String Title
        {
            get { return _title; }
            set { _title = value; }
        }
        public HtmlGenericControl designRegion;
        private ITemplate _content = new DNJTemplate();
        
        [Browsable(false)]
        [MergableProperty(false)]        
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        [TemplateContainer(typeof(DNJDialog))]
        [TemplateInstance(TemplateInstance.Single)]
        public ITemplate Content
        {
            get { return _content; }
            set { _content = value; }
        }


        public override void SaveSettings(ref object stateObject)
        {
            base.SaveSettings(ref stateObject);

            object[] lstObj = new object[2];
            lstObj[0] = Settings;
            lstObj[1] = ClientEvents;

            stateObject = lstObj;

            GenericSettingsObject allSettings = new GenericSettingsObject();

            PageHelper.AppendJQueryScript(ClientEvents.GetDefaultEventsScript(), this.Page.Header);

            Settings.PrepareJSON();
            ClientEvents.PrepareJSON();
            allSettings.Append(Settings);
            allSettings.Append(ClientEvents);

            PageHelper.AppendJQueryPlugin("#" + designRegion.ClientID, "dialog", allSettings.ToString(), this.Page.Header);

        }

        public override void LoadSettings(object stateObject)
        {
            object[] lstObj = (object[])stateObject;
            Settings = (lstObj[0] as DNJDialogSettings);
            ClientEvents = (lstObj[1] as DNJDialogEvents);
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (DesignMode)
            {
                Content.InstantiateIn(this);
            }
        }


        protected override void CreateChildControls()
        {
            Controls.Clear();
            
            designRegion = new HtmlGenericControl("div");
            designRegion.ID = "dialog_" + this.ID;
            designRegion.Attributes.Add("title", Title);



            if (DesignMode)
            {
                HtmlGenericControl div = new HtmlGenericControl("div");
                div.Attributes.Add("style", "position:relative;height:" + this.Height.ToString() + ";overflow:auto");

                HtmlGenericControl titleDiv = new HtmlGenericControl("div");
                titleDiv.Attributes.Add("class", "ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix");
                titleDiv.Attributes.Add("style", "position:relative;width:100%;height:25px");
                HtmlGenericControl titleSpan = new HtmlGenericControl("span");
                titleSpan.Attributes.Add("class", "ui-dialog-title");
                titleSpan.InnerHtml = Title;
                titleDiv.Controls.Add(titleSpan);
                HtmlGenericControl closeBtn = new HtmlGenericControl("a");
                closeBtn.Attributes.Add("href", "#");
                closeBtn.Attributes.Add("class", "ui-dialog-titlebar-close ui-corner-all");
                closeBtn.Attributes.Add("style", "position:absolute;right:3px");
                HtmlGenericControl closeSpan = new HtmlGenericControl("span");
                closeSpan.Attributes.Add("class", "ui-icon ui-icon-closethick");
                closeSpan.InnerHtml = "X";
                closeBtn.Controls.Add(closeSpan);
                titleSpan.Controls.Add(closeBtn);
                Controls.Add(titleDiv);


                int height = Convert.ToInt32(this.Height.Value) - 25;
                designRegion.Attributes.Add("class", "ui-dialog-content ui-widget-content");
                designRegion.Attributes.Add("style", "margin:0;height: "+height.ToString()+"px; width: auto;");

                Content.InstantiateIn(designRegion);

                div.Controls.Add(designRegion);                
                Controls.Add(div);

            }
            else
            {
                Content.InstantiateIn(designRegion);
                Controls.Add(designRegion);
            }
            
            base.CreateChildControls();

        }
    }


}
